package cn.tedu.rabbitmq.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DlxDeclareConfig {
    // 业务交换机名字
    public final static String BIZ_EXCHANGE_NAME = "biz_exchange";
    // 业务队列名字
    public final static String BIZ_QUEUE_NAME = "biz_queue";
    // 死信交换机名字
    private final static String DLX_EXCHANGE_NAME = "dlx_exchange";
    // 死信队列名字
    public final static String DLX_QUEUE_NAME = "dlx_queue";
    // 死信路由key
    private final static String DLX_ROUTING_KEY = "dlx_routing_key";

    @Bean
    public Queue bizQueue(){
        return QueueBuilder.nonDurable(BIZ_QUEUE_NAME)
                .withArgument("x-message-ttl",20000)
                .withArgument("x-dead-letter-exchange",DLX_EXCHANGE_NAME)
                .withArgument("x-dead-letter-routing-key",DLX_ROUTING_KEY).build();
    }
    //声明死信队列
    @Bean
    public Queue dlxQueue(){
        return QueueBuilder.nonDurable(DLX_QUEUE_NAME).build();
    }
    //业务交换机 fanout
    @Bean
    public Exchange bizEx(){
        return ExchangeBuilder.fanoutExchange(BIZ_EXCHANGE_NAME).build();
    }
    //死信交换机 direct
    @Bean
    public Exchange dlxEx(){
        return ExchangeBuilder.directExchange(DLX_EXCHANGE_NAME).build();
    }
    //业务队列和业务交换机绑定
    @Bean
    public Binding bizBinding(){
        return BindingBuilder.bind(bizQueue()).to(bizEx()).with("").noargs();
    }
    //死信队列和死信交换机绑定
    @Bean
    public Binding dlxBinding(){
        return BindingBuilder.bind(dlxQueue()).to(dlxEx()).with(DLX_ROUTING_KEY).noargs();
    }

}
